﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhotogalleryModel;

namespace photogalleryMVC.Controllers
{
    public class PhotoGalleryController : Controller
    {
        
        public PhotoGalleryController ()
        {
            ModelContext = new ModelContext();
            Storage = new ModelStorage();
        }
        
        public ModelStorage Storage { get; private set; }
        public ModelContext ModelContext { get; private set; }

        private AlbumRepository _albumRepository;
        public AlbumRepository AlbumRepository
        {
            get
            {
                _albumRepository = _albumRepository ?? new AlbumRepository(ModelContext);
                return _albumRepository;
            }
        }

        private PhotoRepository _photoRepository;
        public PhotoRepository PhotoRepository
        {
            get
            {
                _photoRepository = _photoRepository ?? new PhotoRepository(Storage, ModelContext);
                return _photoRepository;
            }
        }

        private GalleryUserRepository _galleryUserRepository;
        public GalleryUserRepository UserRepository
        {
            get 
            { 
                _galleryUserRepository = _galleryUserRepository ?? new GalleryUserRepository(ModelContext);
                return _galleryUserRepository;
            }
        }

        private CommentRepository _commentRepository;
        public CommentRepository CommentRepository
        {
            get
            {
                _commentRepository = _commentRepository ?? new CommentRepository(ModelContext);
                return _commentRepository;
            }
        }


        private NewsRepository _newsRepository;
        public NewsRepository NewsRepository
        {
            get { _newsRepository = _newsRepository ?? new NewsRepository(new ModelContext());
                return _newsRepository;
            }
        }

        private SubscribeRepository _subscribeRepository;
        public SubscribeRepository SubscribeRepository
        {
            get { _subscribeRepository = _subscribeRepository ?? new SubscribeRepository(new ModelContext());
                return _subscribeRepository;
            }
        }

        
        protected override void OnException(ExceptionContext filterContext)
        {
            List<string> messages = new List<string>();
            base.OnException(filterContext);
            if(filterContext.Exception is GalleryModelException)
            {
                messages.Add(filterContext.Exception.Message);
            }
            else
            {
                messages.Add("Internal error happens");
            }
            filterContext.ExceptionHandled = true;

            filterContext.Result = View("Error", messages.ToArray());

        }

    }
}
